package com.example.malltest.order.service.impl;

import com.example.malltest.cart.mapper.CartMapper;
import com.example.malltest.common.ServiceCode;
import com.example.malltest.common.ServiceException;
import com.example.malltest.order.entity.Order;
import com.example.malltest.order.mapper.OrderMapper;
import com.example.malltest.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private CartMapper cartMapper;
    /**
     * 结算购物车
     * @param order
     */
    @Override
    public void add(Order order){
        Order order1 = new Order();
        order1.setName(order.getName());
        order1.setPrice(order.getPrice());
        order1.setNum(order.getNum());
        order1.setImage(order.getImage());
        order1.setStatus("待支付");
        Double totalPrice= order.getPrice()*order.getNum().doubleValue();
        order1.setTotalPrice(totalPrice);
        int result = orderMapper.save(order1);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"保存失败！");
        }
        int result1 = cartMapper.delete(order.getId());
        if(result1<0){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"删除失败！");
        }
    }

    /**
     * 批量结算购物车
     * @param orderList
     */
    @Override
    public void batchAdd(List<Order> orderList) {
        for (Order order : orderList) {
            Order order1 = new Order();
            order1.setName(order.getName());
            order1.setPrice(order.getPrice());
            order1.setNum(order.getNum());
            order1.setImage(order.getImage());
            order1.setStatus("待支付");
            Double totalPrice = order.getPrice() * order.getNum().doubleValue();
            order1.setTotalPrice(totalPrice);
            int result = orderMapper.save(order1);
            if (result < 0) {
                throw new ServiceException(ServiceCode.ERR_EXISTS, "保存失败！");
            }
            int result1 = cartMapper.delete(order.getId());
            if(result1<0){
                throw new ServiceException(ServiceCode.ERR_EXISTS,"删除失败！");
            }
        }
    }

    /**
     * 获取订单列表
     * @return
     */
    @Override
    public List<Order> getList(){
        List<Order> orders = orderMapper.getList();
        if(orders == null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在订单！");
        }
        return orders;
    }

    /**
     * 更改状态
     * @param id
     * @param status
     */
    @Override
    public void status(Integer id,String status){
        Order order = orderMapper.getById(id);
        if(order==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该订单！");
        }
        order.setStatus(status);
        int result = orderMapper.edit(order);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"保存失败！");
        }
    }

    /**
     * 保存评价
     * @param id
     * @param commit
     */
    @Override
    public void commit(Integer id,String commit){
        Order order = orderMapper.getById(id);
        if(order==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该订单！");
        }
        order.setCommit(commit);
        int result = orderMapper.edit(order);
        if(result<0){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"保存失败！");
        }
    }

    /**
     * 获取评价
     * @param id
     * @return
     */
    @Override
    public String  getCommit(Integer id){
        Order order = orderMapper.getById(id);
        if(order==null){
            throw new ServiceException(ServiceCode.ERR_EXISTS,"不存在该订单！");
        }
        return order.getCommit();
    }
}
